package com.ikaros.freemarker;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.collections.map.HashedMap;

import javax.management.Attribute;

/**
 * @author: 月光伴奏
 * @Date 2017-10-26 11:25
 */
public class Output {
    public static void main(String[] args) {

        try {
            new Output().gen();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    public void gen() throws IOException, TemplateException{

        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setDirectoryForTemplateLoading(new File("D:/Code/Ikaros/src/main/java/com/ikaros/freemarker/"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        //模板路径
        Template temp = cfg.getTemplate("NewZS.ftl");  // load E:/Work/Freemarker/templates/person.ftl

        // Create the root hash
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("packageName", "com.ikaros.freemarker;");
        root.put("className", "Person");
        root.put("author", "Ricky Fung");
        String json="{\"codeDesc\":\"Success\",\"code\":0,\"message\":\"\",\"availableRegion\":{\"gz\":\"华南地区(广州)\",\"hk\":\"东南亚地区(香港)\",\"gzopen\":\"华南地区(广州OPEN)\",\"sg\":\"东南亚地区(新加坡)\",\"sh\":\"华东地区(上海)\",\"bj\":\"华北地区(北京)\",\"szjr\":\"华南地区(深圳金融)\",\"ca\":\"北美地区(多伦多)\"}}\n";
        JSONObject parse = (JSONObject)JSONObject.parse(json);
        JSONObject availableRegion = (JSONObject) parse.get("availableRegion");
        System.out.println(availableRegion);
        List<Attribute> attr_list = new ArrayList<Attribute>();
        attr_list.add(new Attribute("广州", availableRegion.get("gz")));
        attr_list.add(new Attribute("广州", availableRegion.get("gz")));
        attr_list.add(new Attribute("广州", availableRegion.get("gz")));
        attr_list.add(new Attribute("广州", availableRegion.get("gz")));
        attr_list.add(new Attribute("广州", availableRegion.get("gz")));
        /*List<Attribute> attr_list = new ArrayList<Attribute>();
        attr_list.add(new Attribute("id", "Long"));
        attr_list.add(new Attribute("name", "String"));
        attr_list.add(new Attribute("age", "Integer"));
        attr_list.add(new Attribute("hobby", "List<String>"));*/

        root.put("attrs", attr_list);

//      Writer out = new OutputStreamWriter(System.out);
//      Writer out = new OutputStreamWriter(System.out);
        //生成java路径
        File dir = new File("D:/Code/Ikaros/src/main/java/com/ikaros/freemarker");
        if(!dir.exists()){
            dir.mkdirs();
        }
        OutputStream fos = new  FileOutputStream( new File(dir, "NewZS.json")); //java文件的生成目录
        Writer out = new OutputStreamWriter(fos);
        temp.process(root, out);

        fos.flush();
        fos.close();

        System.out.println("gen code success!");
    }
}
